﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Iit.ITBI.MDSManager;

namespace Iit.ITBI.MDSManagerService
{
    public partial class DebugForm : Form
    {

        MasterEntityReplicator masterEntityReplicator; 


        public DebugForm()
        {
            InitializeComponent();
        }

        private void DebugForm_Load(object sender, EventArgs e)
        {

        }

        private void PrintOutput(string Text)
        {
            AppendText(txtOutput, Text + "\r\n", Color.Black);            
        }

        private void PrintOutput(Exception e)
        {
            AppendText(txtOutput, e.ToString() + "\r\n", Color.Red);            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            masterEntityReplicator = new MasterEntityReplicator();

            txtBoxServiceAddress.Text = masterEntityReplicator.MDSServiceAddress.ToString();
            txtBoxMDSDB.Text = masterEntityReplicator.MDSDBConnectionString;

            masterEntityReplicator.OnInfo += masterEntityReplicator_OnInfo;
            masterEntityReplicator.OnWarning += masterEntityReplicator_OnWarning;
            masterEntityReplicator.OnError += masterEntityReplicator_OnError;

            PrintOutput("Start to replicate");

            backgroundWorker.RunWorkerAsync();

            
        }

        private void masterEntityReplicator_OnInfo(object sender, MessageEventArgs e)
        {
            PrintOutput(e.Message);
        }

        private void masterEntityReplicator_OnError(object sender, MessageEventArgs e)
        {
            PrintOutput(e.Message);
        }

        private void masterEntityReplicator_OnWarning(object sender, MessageEventArgs e)
        {
            PrintOutput(e.Message);
        }

        private void AppendText(RichTextBox box, string text, Color color)
        {
            if (box.InvokeRequired)
            {
                box.Invoke((MethodInvoker)(() => box.ForeColor = color));
                box.Invoke((MethodInvoker)(() => box.AppendText(text)));
            }
            else
            {
                box.ForeColor = color;
                box.AppendText(text);
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                masterEntityReplicator.Replicate();
            }
            catch (Exception ex)
            {
                PrintOutput(ex);
            }

            PrintOutput("Replication ended");          

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void txtBoxMDSDB_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtBoxServiceAddress_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtOutput_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
